
cd "/Users/chuangchen/Library/CloudStorage/OneDrive-UniversityofPittsburgh/LASA"

use "Datos/all_countries3_withcontext.dta", clear

* recode the same-sex marriage question
* moral conservative
gen pro_ssm= 0 if val_ssm <=3
* moral middle
replace pro_ssm=1 if val_ssm <=7 & val_ssm>3
* moral progressive
replace pro_ssm=2 if val_ssm >7
replace pro_ssm=. if val_ssm >10
lab value pro_ssm pro_abort_lab


* pay attention to the case number of eco_moral4
* only 2212 remain
gen eco_moral4 = 1 if pro_ssm == 0 & pro_regulation2==0
replace eco_moral4 = 2 if pro_ssm == 0 & pro_regulation2==1
replace eco_moral4 = 3 if pro_ssm == 0 & pro_regulation2==2
replace eco_moral4 = 4 if pro_ssm == 1 & pro_regulation2==0
replace eco_moral4 = 5 if pro_ssm == 1 & pro_regulation2==1
replace eco_moral4 = 6 if pro_ssm == 1 & pro_regulation2==2
replace eco_moral4 = 7 if pro_ssm == 2 & pro_regulation2==0
replace eco_moral4 = 8 if pro_ssm == 2 & pro_regulation2==1
replace eco_moral4 = 9 if pro_ssm == 2 & pro_regulation2==2
lab value eco_moral4 eco_moral2_lab



gen eco_moral_recode=.



////////////////
* Dalton
////////////////
{
* Calculate the total per election per country.
gen seats=1 // assuming seats = people interviewed
egen votes = total(seats), by(legis pais partido)
egen total_votes = total(seats), by(legis pais)

* Calculate the share of the vote for each party in each election.
gen pervote = votes / total_votes

* Convert pervote to a percentage (optional).
gen pervote_percent = pervote * 100

// I am counting the number of parties by legis
egen unique_id = group(legis pais partido), label
bysort legis pais partido (unique_id): gen tag = _n == 1
bysort legis pais: egen unique_parties = total(tag)
gen unique_parties_less1=unique_parties-1


// Calculate country average 
foreach x of varlist ID1 ID2 {

egen `x'_av= mean(`x'), by(legis pais)
}	

// Calculate by party/legis 
foreach x of varlist ID1 ID2 {

gen `x'_d= pervote_percent*[(`x'-`x'_av)^2]/(unique_parties_less1)
}

foreach x of varlist ID1 ID2 {

egen `x'_dt= sum(`x'_d), by(legis pais tag)
gen `x'_dt2= (`x'_dt)^0.5

egen polarization_`x' = min(`x'_dt2), by(pais legis ) // taking the min because  i only need one by party not all the seats

}	
}


////////////////
*ENP
////////////////
{
* Square the proportionate vote.
gen prop_vote_sq = pervote^2 

* Sum the squared proportions by pais and legis.
egen sum_prop_vote_sq = sum(prop_vote_sq), by(pais legis tag) // I only need one, so the tag indicates the unique cases. 

* Calculate the Effective Number of Parties (ENP).
gen ENP_transition = 1 / sum_prop_vote_sq 

egen ENP = max(ENP_transition), by(pais legis)
	
}

*************************************************************
* Contextual variables

lab variable pid_p "PID"
lab var c_volatilidad "Volatility"
lab var c_polarizaw "Polarization"
lab var c_nepp "Effective Number of Parties"

lab var polarization_ID1 "Polarization"
lab var ENP "Effective Number of Parties"

save "Datos/all_countries3_withcontext2.dta", replace


